package com.youkeyi.ddy.cloud.application.business.storage.in.controller;

import com.youkeyi.ddy.cloud.common.api.application.business.storage.in.BusinessStockInCloudService;
import com.youkeyi.ddy.cloud.common.api.application.business.storage.in.dto.BusinessStockInListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.storage.in.dto.BusinessStockInListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.storage.in.param.BusinessStockInCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.storage.in.param.BusinessStockInListCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.storage.in.StorageStockInCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.storage.in.dto.StorageStockInListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.storage.in.param.StorageStockInCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.storage.in.param.StorageStockInQueryCloudParam;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * BusinessStockInController
 *
 * @Author liuxiawang
 * @Date 2024/3/28 9:11 AM
 * @Location shenzhen.china
 */
@Service
public class BusinessStockInController implements BusinessStockInCloudService {

    @Autowired
    private StorageStockInCloudService storageStockInCloudService;
    
    @Override
    public BusinessStockInListCloudDto list(BusinessStockInListCloudParam param) {
        BusinessStockInListCloudDto dto = new BusinessStockInListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        StorageStockInQueryCloudParam stockInQueryCloudParam = new StorageStockInQueryCloudParam();
        stockInQueryCloudParam.setStockInPrimaryKeys(param.getStockInPrimaryKeys());
        stockInQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
        stockInQueryCloudParam.setBillNos(param.getBillNos());
        stockInQueryCloudParam.setBarCodes(param.getBarCodes());
        stockInQueryCloudParam.setGoodsKeys(param.getGoodsKeys());
        stockInQueryCloudParam.setGoodsNameFuzzy(param.getGoodsNameFuzzy());
        stockInQueryCloudParam.setKeyWordFuzzy(param.getKeyWordFuzzy());
        stockInQueryCloudParam.setPageSize(param.getPageSize());
        stockInQueryCloudParam.setCurrentPage(param.getCurrentPage());
        StorageStockInListCloudDto stockInDtos = storageStockInCloudService.query(stockInQueryCloudParam);
        if (null != stockInDtos && CollectionUtils.isNotEmpty(stockInDtos.getRows())) {
            List<BusinessStockInListItemCloudDto> items = new ArrayList<>();
            stockInDtos.getRows().forEach(stockIn -> {
                BusinessStockInListItemCloudDto item = new BusinessStockInListItemCloudDto();
                item.setStockInPrimaryKey(stockIn.getStockInPrimaryKey());
                item.setCorpKey(stockIn.getCorpKey());
                item.setBillNo(stockIn.getBillNo());
                item.setWarehouseKey(stockIn.getWarehouseKey());
                item.setWarehouseName(stockIn.getWarehouseName());
                item.setBarCode(stockIn.getBarCode());
                item.setGoodsKey(stockIn.getGoodsKey());
                item.setGoodsName(stockIn.getGoodsName());
                item.setKeyWord(stockIn.getKeyWord());
                item.setUnitKey(stockIn.getUnitKey());
                item.setUnitName(stockIn.getUnitName());
                item.setProductDate(stockIn.getProductDate());
                item.setExpirationDate(stockIn.getExpirationDate());
                item.setShelfKey(stockIn.getShelfKey());
                item.setQuantity(stockIn.getQuantity());
                item.setPrice(stockIn.getPrice());
                item.setAdvicePrice(stockIn.getAdvicePrice());
                item.setSupplierKey(stockIn.getSupplierKey());
                item.setSupplierName(stockIn.getSupplierName());
                item.setInStoreType(stockIn.getInStoreType());
                item.setState(stockIn.getState());
                item.setCooperationMode(stockIn.getCooperationMode());
                item.setOrigStockKey(stockIn.getOrigStockKey());
                item.setNotes(stockIn.getNotes());
                item.setPurchaseUserKey(stockIn.getPurchaseUserKey());
                item.setStorageUserKey(stockIn.getStorageUserKey());
                item.setStorageTime(stockIn.getStorageTime());
                item.setFromType(stockIn.getFromType());
                item.setPayType(stockIn.getPayType());
                item.setCreateUserKey(stockIn.getCreateUserKey());
                item.setCreateUserName(stockIn.getCreateUserName());
                item.setCreateTime(stockIn.getCreateTime());
                item.setUpdateUserKey(stockIn.getUpdateUserKey());
                item.setUpdateUserName(stockIn.getUpdateUserName());
                item.setUpdateTime(stockIn.getUpdateTime());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(stockInDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(BusinessStockInCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getGoodsKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        StorageStockInCreateCloudParam stockInCreateCloudParam = new StorageStockInCreateCloudParam();
        stockInCreateCloudParam.setCorpKey(corpKey);
        stockInCreateCloudParam.setBillNo(param.getBillNo());
        stockInCreateCloudParam.setWarehouseKey(param.getWarehouseKey());
        stockInCreateCloudParam.setWarehouseName(param.getWarehouseName());
        stockInCreateCloudParam.setBarCode(param.getBarCode());
        stockInCreateCloudParam.setGoodsKey(param.getGoodsKey());
        stockInCreateCloudParam.setGoodsName(param.getGoodsName());
        stockInCreateCloudParam.setKeyWord(param.getKeyWord());
        stockInCreateCloudParam.setUnitKey(param.getUnitKey());
        stockInCreateCloudParam.setUnitName(param.getUnitName());
        stockInCreateCloudParam.setProductDate(param.getProductDate());
        stockInCreateCloudParam.setExpirationDate(param.getExpirationDate());
        stockInCreateCloudParam.setShelfKey(param.getShelfKey());
        stockInCreateCloudParam.setQuantity(param.getQuantity());
        stockInCreateCloudParam.setPrice(param.getPrice());
        stockInCreateCloudParam.setAdvicePrice(param.getAdvicePrice());
        stockInCreateCloudParam.setSupplierKey(param.getSupplierKey());
        stockInCreateCloudParam.setSupplierName(param.getSupplierName());
        stockInCreateCloudParam.setInStoreType(param.getInStoreType());
        stockInCreateCloudParam.setState(param.getState());
        stockInCreateCloudParam.setCooperationMode(param.getCooperationMode());
        stockInCreateCloudParam.setOrigStockKey(param.getOrigStockKey());
        stockInCreateCloudParam.setNotes(param.getNotes());
        stockInCreateCloudParam.setPurchaseUserKey(param.getPurchaseUserKey());
        stockInCreateCloudParam.setStorageUserKey(param.getStorageUserKey());
        stockInCreateCloudParam.setStorageTime(param.getStorageTime());
        stockInCreateCloudParam.setFromType(param.getFromType());
        stockInCreateCloudParam.setPayType(param.getPayType());
        stockInCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        stockInCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        return storageStockInCloudService.create(stockInCreateCloudParam);
    }
}
